#include<stdio.h>
#include<iostream>
#include<fstream>
#include <string.h>

#include <atmi.h>
#include <fml.h>
#include <fml1632.h>
#include <userlog.h>
#include <xa.h>
#include <tpadm.h>

using namespace std;

int geterr()
{
	switch(tperrno)
	{
		case TPEINVAL:
			printf("TPEINVAL\n");
			break;
		case TPEOTYPE:
			printf("TPEOTYPE\n");
			break;
		case TPETIME:
			printf("TPETIME\n");
			break;
		case TPEBLOCK:
			printf("TPEBLOCK\n");
			break;
		case TPENOENT:
			printf("TPENOENT\n");
			break;
		case TPEEVENT:
			printf("TPEEVENT\n");
			break;
		case TPEPERM:
			printf("TPEPERM\n");
			break;
		case TPEPROTO:
			printf("TPEPROTO\n");
			break;
		case TPESYSTEM:
			printf("TPESYSTEM\n");
			break;
		case TPEOS:
			printf("TPEOS\n");
			break;
	}
	return 0;
}

int main() 
{
	printf("Hello World\n"); /* prints Hello World*/
	TPINIT *tpinfo;

		char * lBuffer;
		char* bufferType;
		char * sType;
		long size;

		char service[100];
		char *sendBuffer;
		long sendLength;
		long flags;
		char *recvBuffer;
		long recvLength;
		char Buffer[1024000];
//		FILE * myfile;


//		myfile=fopen("example.xml","r");
		ifstream myfile ("example.xml", fstream::in| fstream::binary);



try
{
	sendBuffer = NULL;
	recvBuffer = NULL;

	memset(Buffer, 0, 1024000);


	if ((tpinfo = (TPINIT *)tpalloc("TPINIT",(char *)NULL,
		     TPINITNEED(8))) == (TPINIT *)NULL)
	{
		printf("tpalloc error\n");
	}
	int retCode = tpinit(tpinfo);
        if(retCode==-1)
        {
                printf("tpinit error\n");
        }

	sendLength = 1024000;
	if ((sendBuffer = (char *)tpalloc("XML",(char *)NULL,
				sendLength+1 )) == (char *)NULL)
	{
		printf("tpalloc error\n");
	}

	recvLength = 1024000;
	if ((recvBuffer = (char *)tpalloc("XML",(char *)NULL,
				recvLength+1 )) == (char *)NULL)
	{
		printf("tpalloc error\n");
	}
	memset(recvBuffer, 0, sizeof(recvBuffer));


	if (myfile.is_open())
	{
		myfile.read(Buffer,sendLength);
		printf("%s\n",Buffer);
		sendLength = myfile.gcount();
		memcpy(sendBuffer, Buffer, sendLength+2);
		myfile.close();
	}


		sprintf(service,"RtmasFncs");
		flags = TPRECVONLY;
		int cd;
		int ret;
		long revent;

		printf("sendLength=%ld\n",sendLength);

		if(1==1)
		{
			cd = tpconnect(service, sendBuffer, 1024000+1, flags);
			if(cd==-1)
			{
				printf("tpconnect error\n");
				geterr();
				return -1;
			}


			flags = TPRECVONLY ;
			sendLength = strlen(sendBuffer);
			printf("sendLength=%ld\n",sendLength);
			revent = 0;
			//cout<<sendBuffer;
			//sprintf(sendBuffer,"hello");

			//ret = tpsend(cd, sendBuffer, 1002400+1, flags, &revent);
			if(ret==-1)
			{
				printf("tpsend error\n");
				geterr();
				return -1;
			}

			printf("tpsend revent=%ld\n",revent);

			flags = TPNOCHANGE;

			do
			{
				//ret = tprecv(connectId, (char**)&iBuf, &iBufLen, TPNOCHANGE, &revent);
				ret = tprecv(cd, &recvBuffer, &recvLength, flags, &revent);
			//	if(ret == -1 && revent == TPRECVONLY )
			//	{
					//flags = 0;
		//			ret = tprecv(cd, &recvBuffer, &recvLength, flags, &revent);
			//	}
			//	if( ret == -1 && tperrno==TPEEVENT && revent==TPEV_SVCSUCC  )
			//	{
			//		cerr<<"tprecv error"<<tperrno<<endl;
		//			geterr();
		//			return -1;
		//		}

			}
			while(ret == -1 && revent == TPRECVONLY);

			printf("ret = %d,revent=%ld,recvLength=%ld\n",ret,revent,recvLength);

			ret = tpdiscon(cd);
			if(ret==-1)
			{
				printf("tpdiscon error\n");
				geterr();
			}
		}
		else
		{
			ret = tpcall(service, sendBuffer, sendLength, &recvBuffer, &recvLength, 0);
			if(ret == -1) 
			{
				printf("Can't send request to service %s\n",service);
				printf ("Tperrno = %d\n",tperrno);	
				tpterm();
				return -1;
			 }
		}


		recvBuffer[recvLength+1]='\0';
		printf("%s\n",recvBuffer);

		tpfree(sendBuffer);
		tpfree(recvBuffer);

}
catch(...)
{
	tpfree(sendBuffer);
}




	return 0;
}
